package com.hngs.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hngs.dao.CategoryDao;
import com.hngs.dao.ProductDao;
import com.hngs.info.PageInfo;
import com.hngs.model.Category;
import com.hngs.model.CategoryQuery;
import com.hngs.model.Product;
import com.hngs.model.ProductQuery;
import com.hngs.threadlocal.LocalPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//业务逻辑层  主要处理dao获取得数据库数据 m  C
@Service
public class ProductService {
    @Autowired
    private ProductDao productDao;

    //查询所有 包含条件查询
    public List<Product> select(ProductQuery query){
        if(query.getPageNum()!=null && query.getPageSize()!=null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }

        List<Product> select = productDao.select(query);
        if(select instanceof  Page){
            //强制类型转换
            Page page=(Page) select;
            long total = page.getTotal();
            LocalPageUtils.set(PageInfo.builder().total(total).build());
        }

        return select;
    }

    public Integer insert(Product product) {

        return  productDao.insert(product);
    }

    public Integer update(Product product) {
        return productDao.update(product);
    }

    public Integer delete(Integer id){
        return productDao.updateStatus(id,0);
    }
}
